package src.com.util;

import java.sql.*;

public class DBOmanagerUsers {
    public static final String DRIVETR = "com.mysql.jdbc.Driver";
    public static final String USER = "root";
    public static final String PASS = "0000";
    public static final String URL = "jdbc:mysql://192.168.1.113:3306/CRAI";

    private static DBOmanagerUsers per = null;
    private Connection conn = null;
    private PreparedStatement ps = null;


    public static DBOmanagerUsers createInstance(){
        if(per == null){
            per = new DBOmanagerUsers();
            per.initDB();
        }
        return per;
    }

    public void initDB(){
        try {
            Class.forName(DRIVETR);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    public void getConn(){
        try {
            conn = DriverManager.getConnection(URL,USER,PASS);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void closeDB(){
        try {
            if(ps!=null)
            ps.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if(conn!=null)
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //增删改
    public int execueUpdate(String sql){
        int result = 0;
        try {
            result = ps.executeUpdate(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    public PreparedStatement createPrepareStatement(String sql){
        try {
            ps = conn.prepareStatement(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return ps;
    }
}
